package com.yswy.app.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yswy.app.annotation.NotAuth;
import com.yswy.app.config.WebUtils;
import com.yswy.app.service.AppUserService;
import com.yswy.common.enums.ResultStatus;
import com.yswy.common.exception.LoginException;
import com.yswy.common.model.ResultModel;
import com.yswy.domain.entity.AppUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component
public class Interceptor extends HandlerInterceptorAdapter {
    @Resource
    private AppUserService appUserService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
//        if (o instanceof HandlerMethod) {
//            ObjectMapper mapper = new ObjectMapper();
//
//            String reqAddress = request.getRemoteAddr();
//            log.info(reqAddress + " 安全过滤器：" + request.getRequestURI() + " METHOD:" + request.getMethod());
//            String requestURI = request.getRequestURI();
//            log.info(requestURI);
//            HandlerMethod method = (HandlerMethod) o;
//
//            // 获取方法上有没有打注解
//            NotAuth notAuth = method.getMethodAnnotation(NotAuth.class);
//            //不为null表示该方法打了注解
//
//            if (request.getRequestURI().contains("swagger")
//                    || request.getRequestURI().contains("druid")) {
//                return true;
//            }
//
//            if (notAuth != null) {
//                log.info("不拦截： ");
//                return true;
//            }else {
//                AppUser user = null;
//                String token = request.getHeader("Authorization");
//                log.info(reqAddress + " Authorization : " + token);
//                //更新用户的TOKEN 过期时间，这样用户只要一直有操作，就永远不需要登录。
//                try{
//                    user = appUserService.getUserByAuthToken(token);
//                }catch (LoginException e){
//                    responseFail(response);
//                    return false;
//                }
//                if (user!= null){
//                    request.getSession().setAttribute("user", user);
//                }else {
//                    responseFail(response);
//                    return false;
//                }
//            }
//        }
        return true;
    }


    /**
     * 直接向前端抛出异常
     */
    private void responseFail(HttpServletResponse response)  {
        ResultModel resultData = ResultModel.error(ResultStatus.LOGIN_OVER);
        WebUtils.writeJson(response,resultData);
    }

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {
//        System.out.println("拦截器1 postHandle： ");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest,
                                HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
//        System.out.println("拦截器1 afterCompletion： ");
    }

}
